package com.fallsvc.re.networkprogramming.base;

import java.util.Random;

public class ThreadDemo {
    private static class MyThread extends Thread {
        @Override
        public void run() {
            Random random = new Random();
            while (true) {
        // 打印线程名称
                System.out.println(Thread.currentThread().getName());
                try {
        // 随机停⽌运⾏ 0-9 秒
                    Thread.sleep(random.nextInt(10));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main1(String[] args) {
        MyThread t1 = new MyThread();
        t1.start();
        Random random = new Random();
        while (true) {
        // 打印线程名称
            System.out.println(Thread.currentThread().getName());
            try {
                Thread.sleep(random.nextInt(10));
            } catch (InterruptedException e) {
        // 随机停⽌运⾏ 0-9 秒
                e.printStackTrace();
            }
        }

    }

    public static void main2(String[] args) {
        Thread thread1 = new Thread() {
            @Override
            public void run() {
                System.out.println("thread1");
            }
        };
        thread1.start();


        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread2");
            }
        });
        thread2.start();

        Thread thread3 = new Thread(() -> System.out.println("thread3"));

        thread3.start();
    }

    public static void main3(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
           while(!Thread.interrupted()) {
               System.out.println("操作中...");
               try{
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   System.out.println("有内鬼终止交易");
                   e.printStackTrace();
                   break;
               }
           }
            System.out.println("幸好没操作");
        });
        System.out.println(thread.getState());

        thread.start();
        Thread.sleep(10000);
        thread.interrupt();
        Thread.sleep(2000);
        System.out.println(thread.getState());
    }

    public static void main4(String[] args) {
        Thread thread = Thread.currentThread();
        System.out.println(thread.getName());
    }

    public static void main5(String[] args) throws InterruptedException {
        System.out.println(System.currentTimeMillis());
        Thread.sleep(3 * 1000);
        System.out.println(System.currentTimeMillis());
    }
    public static void main6(String[] args) {
        for (Thread.State state : Thread.State.values()) {
            System.out.println(state);
        }
    }
    public static void main7(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            for (int i = 0; i < 1000_0000; i++) {}
        }, "李四");
        System.out.println(t.getName() + ": " + t.getState());
        t.start();
        while (t.isAlive()) {
            System.out.println(t.getName() + ": " + t.getState());
        }
        System.out.println(t.getName() + ": " + t.getState());
    }

    public static void main(String[] args) {
        final Object object = new Object();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object) {
                    while (true) {
                        try {
//                            Thread.sleep(1000);
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "t1");
        t1.start();
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    synchronized (object) {
                        System.out.println("hehe");
                    }
                }
            }
        }, "t2");
        t2.start();
    }
}